package com.splant.android.smart.BusinessLogicLevel;

import android.app.Activity;
import android.graphics.RectF;
import android.util.Size;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.splant.android.smart.ModelLevel.DetectorListBean;
import com.splant.android.smart.ModelLevel.DetectorMapObject;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.ModelLevel.WaterMapObject;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Dimen_Util;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Administrator on 2018/1/4.
 */

public class MapManager {

    private List<WaterMapObject> waterMapObjectList = new ArrayList<>();
    private List<DetectorMapObject> detectorMapObjectList = new ArrayList<>();
    private List<View> viewList = new ArrayList<>();
    private FrameLayout fl_MapFrame;
    private Size mMapSize;
    private RectF rect;
    private Activity activity;

    public MapManager(Activity activity, FrameLayout fl_MapFrame) {

        this.activity = activity;
        this.fl_MapFrame = fl_MapFrame;
    }

    public void setMapSize(Size mapSize)
    {
        mMapSize=mapSize;
    }

    public void setMapRect(RectF rect,boolean isFirst) {

        this.rect = rect;

        if (!isFirst)
            updateDevicesPostion();

    }

    private void updateDevicesPostion()
    {
        for (WaterMapObject waterMapObject : waterMapObjectList) {

            int left = (int) (rect.left+ rect.width() * waterMapObject.getLeftScale());
            int top  = (int) (rect.top+ rect.height() * waterMapObject.getTopScale());

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(System_Dimen_Util.dip2px(activity, 40), ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(left, top, 0, 0);

            waterMapObject.getWaterView().setLayoutParams(params);

        }

        for (DetectorMapObject detectorMapObject : detectorMapObjectList) {

            int left = (int) (rect.left + rect.width() * detectorMapObject.getLeftScale());
            int top  = (int) (rect.top+ rect.height() * detectorMapObject.getTopScale());

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(left, top, 0, 0);

            detectorMapObject.getDetectorView().setLayoutParams(params);
        }

    }

    public void removeAllDeviceView()
    {
        for (View view:viewList)
            fl_MapFrame.removeView(view);
    }

    public void addWaterList(List<WaterListBean.Water> waterList) {

        if (waterList!=null)
        {
            waterMapObjectList.clear();

            for (WaterListBean.Water water : waterList) {

                WaterMapObject waterMapObject = new WaterMapObject(activity, water);

                float leftScale = ((float) water.getPx() / mMapSize.getWidth());
                float topScale = ((float) water.getPy() / mMapSize.getHeight());

                waterMapObject.setLeftScale(leftScale);
                waterMapObject.setTopScale(topScale);

                addDeviceView(leftScale, topScale, waterMapObject.getWaterView(), System_Dimen_Util.dip2px(activity, 40));

                waterMapObjectList.add(waterMapObject);
            }
        }
    }

    private void addDeviceView(float leftScale, float topScale, View view, int width) {

        int left = (int) (rect.left+ rect.width() * leftScale);
        int top  =  (int)  (rect.top + rect.height()* topScale);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(left, top, 0, 0);
        view.setLayoutParams(params);

        fl_MapFrame.addView(view);
        viewList.add(view);
    }

    public void addDetectorList(List<DetectorListBean.Detector> detectorList) {

        if (detectorList != null) {

            detectorMapObjectList.clear();

            for (DetectorListBean.Detector detector : detectorList) {

                DetectorMapObject detectorMapObject = new DetectorMapObject(activity, detector);

                float leftScale = ((float) detector.getPx() / mMapSize.getWidth());
                float topScale = ((float) detector.getPy() / mMapSize.getHeight());

                detectorMapObject.setLeftScale(leftScale);
                detectorMapObject.setTopScale(topScale);

                addDeviceView(leftScale, topScale, detectorMapObject.getDetectorView(), ViewGroup.LayoutParams.WRAP_CONTENT);

                detectorMapObjectList.add(detectorMapObject);
            }

        }
    }


    public void allWatersOpenedSwitchs()
    {
        if (waterMapObjectList!=null)
        {
            for (WaterMapObject waterMapObject:waterMapObjectList)
            {
                waterMapObject.allSwitchsOpened();
            }
        }
    }
    public void allWaterClosedSwitchs()
    {
        if (waterMapObjectList!=null)
        {
            for (WaterMapObject waterMapObject:waterMapObjectList)
            {
                waterMapObject.allSwitchsClosed();
            }
        }
    }
    public void watersOpenedFailure(String waterIds,int waterLength)
    {
        if (waterMapObjectList!=null)
        {
            for (int i=0;i<waterMapObjectList.size();i++)
            {
                WaterMapObject waterMapObject=waterMapObjectList.get(i);
                waterMapObject.openSwitchsFailure(waterIds,waterLength);
            }
        }
    }

    public void showHideDeviceName(boolean b)
    {
        for (WaterMapObject waterMapObject:waterMapObjectList)
            waterMapObject.showHideDeviceName(b);
        for (DetectorMapObject detectorMapObject:detectorMapObjectList)
            detectorMapObject.showHideDeviceName(b);
    }

}
